var express = require("express");
var router = express.Router();
var url = require("url");
var success = require("./data/success.json");
var error = require("./data/error.json");
//mysql
var mysql = require('mysql');
//mysql本身提供连接数据库的方法：createConnection
var Client = mysql.createConnection({
  host:'localhost',
  user:'root',
  password:'',
  database:'web1806'
})
//操作数据库：query
var mysqlfn = function(sql,arr,callback){
  Client.query(sql,arr,function(error,result){
    if(error){
        return;
    }
    callback(result);
  })
}
//登陆的get请求
router.get('/glogin',function(req,res){
    var sql='select uname from myhaha';
    mysqlfn(sql,null,function(result){
        res.send(result);
    })
})

//登录
router.post('/login',function(req,res){
  var uname = req.body.uname;
  var psword=req.body.psword;
  var sql='select * from myhaha where uname=? and psword=?';
  var arr=[uname,psword];
  mysqlfn(sql,arr,function(result){
    if(result.length > 0){
      res.send(result);
    }else{
      res.send(error);
    }
  })
})
//注册
router.post('/register',function(req,res){
  var uname = req.body.uname;
  var psword=req.body.psword;
  var sql='insert into myhaha values(null,?,?)';
  var arr=[uname,psword];
  mysqlfn(sql,arr,function(result){
    if(result.affectedRows){
      res.send(success);
    }else{
      res.send(error);
    }
  })
})



var personInfo = require('./JsonData/PersonInfo/personInfo.json')
var sportsData = require('./JsonData/SportsData/sportsData.json')
var classes = require('./JsonData/Classes/classes.json')
var runData = require('./JsonData/RunData/runData.json')
var music = require('./JsonData/Music/music.json')
var rank = require('./JsonData/Rank/rank.json')
var store = require('./JsonData/Store/store.json')
var trains = require('./JsonData/Trains/trains.json')
var coach = require('./JsonData/Coach/coach.json')
var dongtai = require('./JsonData/Dongtai/dongtai.json')
var focuses = require('./JsonData/Focuses/focuses.json')
var nearly = require('./JsonData/Nearly/nearly.json')
var foods = require('./JsonData/Foods/foods.json')
var guide = require('./JsonData/Guide/guide.json')

//个人信息
router.get('/guide', function(req,res) {
  res.json(guide);
});

//个人信息
router.get('/personinfo', function(req,res) {
  res.json(personInfo);
});

//走路 跑步 骑行
router.get('/sportsData', function(req,res) {
  res.json(sportsData);
});

//跑步数据
router.get('/runData', function(req,res) {
  res.json(runData);
});

//走路 跑步 骑行 排行
router.get('/rank', function(req,res) {
  res.json(rank);
});

//马甲 臀部
router.get('/trains', function(req,res) {
  res.json(trains);
});

//教练
router.get('/coach', function(req,res) {
  res.json(coach);
});

//课程
router.get('/classes', function(req,res) {
  res.json(classes);
});

//健身 素材
router.get('/music', function(req,res) {
  res.json(music);
});

//饮食
router.get('/foods', function(req,res) {
  res.json(foods);
});

//商城
router.get('/store', function(req,res) {
  res.json(store);
});

//动态
router.get('/dongtai', function(req,res) {
  res.json(dongtai);
});

//关注
router.get('/focus', function(req,res) {
  res.json(focuses);
});

//附近
router.get('/nearly', function(req,res) {
  res.json(nearly);
});

//附近
router.get('/nearly', function(req,res) {
  res.json(nearly);
});

var userInfo = require('./Login/login')
var userError = require('./Login/error')
router.get('/userInfo', function(req,res) {
  res.send(userInfo);
});



module.exports = router;
